package cn.youframe.sample.ajaxjson;

import cn.youframe.frame.base.BaseCtrl;
import cn.youframe.frame.beans.AjaxVo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * User: Administrator
 * DateTime: 12-6-24 下午9:59
 * Desc:
 */
@Controller
@RequestMapping(value = "/sample")
public class AjaxJsonCtrl extends BaseCtrl {
    /**
     * 获得Map参数，返回PolicyVo对象
     * @param parasMap
     * @return
     */
    @RequestMapping(value = "/AjaxJsonCtrl!getPolicyInfo.do")
    public @ResponseBody PolicyVo getPolicyInfo(@RequestParam Map<String,String> parasMap){
        logger.info(parasMap.get("para1"));
        logger.info(parasMap.get("para2"));
        PolicyVo policyVo=new PolicyVo();
        policyVo.setPolicyId("PID002200");
        policyVo.setPolicyNo("PNO990089");
        policyVo.setPolicyType("交强险");
        policyVo.setStartDate("2012-06-25 12:12:45");

        Map carVo=new HashMap();
        carVo.put("carId","CID9900011");
        carVo.put("carLicenseNo","沪A3387");
        carVo.put("carFrameNo","F88999443");
        carVo.put("carType","家庭自用");
        policyVo.setCarVo(carVo);

        SpInfoVo[] spInfoVo=new SpInfoVo[5];
        for(int i=0;i<5;i++){
            SpInfoVo spVo=new SpInfoVo();
            spVo.setSeqId("SPID-"+i);
            spVo.setSpNo("SPNO-"+i);
            spVo.setSpName("SPName-"+1);
            spInfoVo[i]=spVo;
        }
        policyVo.setSpInfoVo(spInfoVo);

        InsureVo[] insureVos=new InsureVo[6];
        for(int k=0;k<6;k++){
            InsureVo insureVo=new InsureVo();
            insureVo.setInsureId("IID_"+k);
            insureVo.setInsureName("主险_"+k);
            InsureChildVo[] insureChildVo=new InsureChildVo[3];
            for(int j=0;j<3;j++){
                InsureChildVo insureChild=new InsureChildVo();
                insureChild.setInsureId("INSUID-"+j);
                insureChild.setInsureName(k+"_子险_"+j);
                insureChildVo[j]=insureChild;
            }
            insureVo.setInsureChildVos(insureChildVo);
            insureVos[k]=insureVo;
        }
        policyVo.setInsureVo(insureVos);
        return policyVo;
    }

    /**
     * json方式传参数,传一个对象
     * @param parasMap
     * @return
     */
    @RequestMapping(value = "/AjaxJsonCtrl!putPolicyInfo.do")
    public @ResponseBody PolicyVo putPolicyInfo(@RequestParam Map<String,String> parasMap){
        logger.info("putPolicyInfo:SeqId="+parasMap.get("para1")+",SpNo="+parasMap.get("para2")+",SpName="+parasMap.get("para3")+",SpContent="+parasMap.get("para4"));
        PolicyVo policy=new PolicyVo();
        return policy;
    }
    /**
     * json方式传参数,传一个嵌套对象
     * @param policyVo
     * @return
     */
    @RequestMapping(value = "/AjaxJsonCtrl!policyInfo.do")
    public @ResponseBody PolicyVo policyInfo(PolicyVo policyVo){
        logger.info("policyVo:PolicyId="+policyVo.getPolicyId()+",PolicyNo="+policyVo.getPolicyNo());
        PolicyVo policy=new PolicyVo();
        return policy;
    }
    /**
     * json方式传参数,传List
     * @param spInfoLs
     * @return
     */
    @RequestMapping(value = "/AjaxJsonCtrl!listPara.do")
    public @ResponseBody SpInfoVo listPara(@RequestBody List<Map<String,String>> spInfoLs){
        SpInfoVo spInfo=new SpInfoVo();
        try{
            logger.info("spInfoLs length="+spInfoLs.size());
            for(Map spInfoVo:spInfoLs){
                logger.info("SeqId="+spInfoVo.get("seqId")+",SpNo="+spInfoVo.get("spNo")+",SpName="+spInfoVo.get("spName")+",SpContent="+spInfoVo.get("spContent"));
            }
        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }
        return spInfo;
    }
    @RequestMapping(value = "/AjaxJsonCtrl!ajaxForm.do")
    public @ResponseBody AjaxVo ajaxForm(HttpServletRequest request){
        AjaxVo ajaxVo=new AjaxVo();
        ajaxVo.setSeqId(request.getParameter("para1"));
        return ajaxVo;
    }
}
